package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取学生课程安排通知信息
func GetWeekCourseNoticeInfo(p map[string]interface{}) *models.WeekCourseNotice {
	o := orm.NewOrm()
	weekCourseNotice := &models.WeekCourseNotice{}
	qs := o.QueryTable("WeekCourseNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(weekCourseNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if weekCourseNotice.Id > 0 {
			weekCourseNotice.CreatedShowDate = weekCourseNotice.Created.Format("2006.1.2")
			weekCourseNotice.CreatedShow = weekCourseNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	return weekCourseNotice
}

// 添加多条学生课程安排通知
func AddWeekCourseNotices(input url.Values) (int64, error) {
	o := orm.NewOrm()
	var weekCourseNotices []models.WeekCourseNotice
	weekCourse := &models.WeekCourse{}
	if _, ok := input["week_course_id"]; ok {
		weekCourse.Id = utils.Atoi64(input["week_course_id"][0])
		o.Read(weekCourse)
	}
	p := map[string]interface{}{}
	p["classId"] = input["class_id"][0]
	_, studentList := GetStudentList(p)
	for _, student := range studentList {
		if student.Status == 1 {
			weekCourseNotice := models.WeekCourseNotice{}
			weekCourseNotice.WeekCourse = weekCourse
			weekCourseNotice.Student = student
			weekCourseNotices = append(weekCourseNotices, weekCourseNotice)
		}
	}
	return o.InsertMulti(1, weekCourseNotices)
}

// 修改学生课程安排通知信息
func EditWeekCourseNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("WeekCourseNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取学生课程安排通知列表
func GetWeekCourseNoticeList(p map[string]interface{}) (int64, []*models.WeekCourseNotice) {
	var weekCourseNoticeList []*models.WeekCourseNotice
	o := orm.NewOrm()
	qs := o.QueryTable("WeekCourseNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&weekCourseNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, weekCourseNotice := range weekCourseNoticeList {
			weekCourseNotice.CreatedShowDate = weekCourseNotice.Created.Format("2006.1.2")
			weekCourseNotice.CreatedShow = weekCourseNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, weekCourseNotice := range weekCourseNoticeList {
			weekCourseNotice.StudentName = weekCourseNotice.Student.RealName
			weekCourseNotice.TeacherName = weekCourseNotice.WeekCourse.User.RealName
			weekCourseNotice.TeacherPhone = weekCourseNotice.WeekCourse.User.Phone
			weekCourseNotice.MondayMorningCourse = weekCourseNotice.WeekCourse.MondayMorning
			weekCourseNotice.MondayAfternoonCourse = weekCourseNotice.WeekCourse.MondayAfternoon
			weekCourseNotice.TuesdayMorningCourse = weekCourseNotice.WeekCourse.TuesdayMorning
			weekCourseNotice.TuesdayAfternoonCourse = weekCourseNotice.WeekCourse.TuesdayAfternoon
			weekCourseNotice.WednesdayMorningCourse = weekCourseNotice.WeekCourse.WednesdayMorning
			weekCourseNotice.WednesdayAfternoonCourse = weekCourseNotice.WeekCourse.WednesdayAfternoon
			weekCourseNotice.ThursdayMorningCourse = weekCourseNotice.WeekCourse.ThursdayMorning
			weekCourseNotice.ThursdayAfternoonCourse = weekCourseNotice.WeekCourse.ThursdayAfternoon
			weekCourseNotice.FridayMorningCourse = weekCourseNotice.WeekCourse.FridayMorning
			weekCourseNotice.FridayAfternoonCourse = weekCourseNotice.WeekCourse.FridayAfternoon
			weekCourseNotice.SaturdayMorningCourse = weekCourseNotice.WeekCourse.SaturdayMorning
			weekCourseNotice.SaturdayAfternoonCourse = weekCourseNotice.WeekCourse.SaturdayAfternoon
			weekCourseNotice.SundayMorningCourse = weekCourseNotice.WeekCourse.SundayMorning
			weekCourseNotice.SundayAfternoonCourse = weekCourseNotice.WeekCourse.SundayAfternoon
			weekCourseNotice.CreatedShowDate = weekCourseNotice.Created.Format("2006.1.2")
		}
	}
	return totalCount, weekCourseNoticeList
}
